package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Objects;

public class NewAccountServlet extends HttpServlet {
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    public static final String NEW_ACCOUNT_FORM = "/WEB-INF/jsp/account/newAccount.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession=req.getSession();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String repeatedPassword=req.getParameter("repeatedPassword");
        String checkcode=req.getParameter("checkCode");
        String code= (String) req.getSession().getAttribute("randCheckCode");
        AccountService accountService=new AccountService();
        if (checkcode.equals("")){
            httpSession.setAttribute("message","The Verification code cannot be empty!");
            req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req,resp);
            return;
        }
        else {
            if (!checkcode.equalsIgnoreCase(code)) {
                httpSession.setAttribute("message", " Incorrect the verification code!");
                req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req, resp);
                return;
            } else {
                if (accountService.getAccount(username) != null) {
                    httpSession.setAttribute("message", "The account has been registered!");
                    req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req, resp);
                    return;
                }
                if (Objects.equals(username, "") || Objects.equals(password, "")) {
                    httpSession.setAttribute("message", "The account or password cannot be empty!");
                    req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req, resp);
                    return;
                }
                if (!Objects.equals(password, repeatedPassword)) {
                    httpSession.setAttribute("message", "The two passwords are inconsistent!");
                    req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req, resp);
                    return;
                }
            }
        }
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        if (Objects.equals(req.getParameter("account.firstName"), "") ||
                Objects.equals(req.getParameter("account.lastName"), "") ||
                Objects.equals(req.getParameter("account.email"), "") ||
                Objects.equals(req.getParameter("account.phone"), "") ||
                Objects.equals(req.getParameter("account.address1"), "") ||
                Objects.equals(req.getParameter("account.city"), "") ||
                Objects.equals(req.getParameter("account.state"), "") ||
                Objects.equals(req.getParameter("account.zip"), "") ||
                Objects.equals(req.getParameter("account.country"), "")) {
            httpSession.setAttribute("messageWarning", "Information other than Address2 cannot be empty");
            httpSession.removeAttribute("message");
            req.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(req, resp);
            return;
        }
        account.setFirstName(req.getParameter("account.firstName"));
        account.setLastName(req.getParameter("account.lastName"));
        account.setEmail(req.getParameter("account.email"));
        account.setPhone(req.getParameter("account.phone"));
        account.setAddress1(req.getParameter("account.address1"));
        account.setAddress2(req.getParameter("account.address2"));
        account.setCity(req.getParameter("account.city"));
        account.setState(req.getParameter("account.state"));
        account.setZip(req.getParameter("account.zip"));
        account.setCountry(req.getParameter("account.country"));
        account.setLanguagePreference(req.getParameter("account.languagePreference"));
        account.setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
        account.setListOption(req.getParameterValues("account.listOption") != null);
        account.setBannerOption(req.getParameterValues("account.bannerOption") != null);
        accountService.insertAccount(account);
        httpSession.removeAttribute("messageWarning");
        httpSession.setAttribute("message", "Registered successfully, please log in!");
        req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
    }
}
